<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-row>
            <a-col :span="24">
              <a-form-item label="自定义项目编号" v-bind="validateInfos.zcOnly">
                <a-input v-model:value="formData.zcOnly" placeholder="请输入项目编号"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="项目编号" v-bind="validateInfos.zcOnly">
                <a-select v-model:value="formData.zcOnly" style="width: 200px" placeholder="请选择项目编号" allow-clear>
                  <a-select-option v-for="(item, index) in list3" :key="index" :value="item">{{ item }} </a-select-option>
                </a-select>
<!--                <a-button type="primary" @click="handleSearch">查询</a-button>-->
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="型号规格" v-bind="validateInfos.zcName">
                <a-input v-model:value="formData.zcName" placeholder="请输入型号规格"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="预计出库" v-bind="validateInfos.zcYuji">
                <a-input v-model:value="formData.zcYuji" placeholder="请输入预计出库"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
<!--            <a-col :span="24">-->
<!--              <a-form-item label="实际出库" v-bind="validateInfos.zcShiji">-->
<!--                <a-input v-model:value="formData.zcShiji" placeholder="请输入实际出库"  allow-clear ></a-input>-->
<!--              </a-form-item>-->
<!--            </a-col>-->
            <a-col :span="24">
              <a-form-item label="实际出库" v-bind="validateInfos.zcShiji">
                <a-input v-model:value="实际出库请到应收修改" placeholder="实际出库请到应收修改"  disabled></a-input>
              </a-form-item>
            </a-col>
<!--            <a-col :span="24">-->
<!--              <a-form-item label="单位规格" v-bind="validateInfos.zcDanwei">-->
<!--                <a-input v-model:value="formData.zcDanwei" placeholder="请输入单位规格"  allow-clear ></a-input>-->
<!--              </a-form-item>-->
<!--            </a-col>-->
            <a-col :span="24">
              <a-form-item label="单位规格" v-bind="validateInfos.zcDanwei">
                <a-select v-model:value="formData.zcDanwei" placeholder="请选择单位规格" allow-clear>
                  <a-select-option value="箱">箱</a-select-option>
                  <a-select-option value="件">件</a-select-option>
                  <a-select-option value="套">套</a-select-option>
                  <a-select-option value="个">个</a-select-option>
                  <!-- Add more options as needed -->
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="单价" v-bind="validateInfos.zcOld">
                <a-input v-model:value="formData.zcOld" placeholder="请输入单价"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="创建时间" v-bind="validateInfos.creatTime">
                <a-input v-model:value="formData.creatTime" placeholder="请输入创建时间"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
						<a-col :span="24">
							<a-form-item label="备注" v-bind="validateInfos.zcLittle">
								<a-input v-model:value="formData.zcLittle" placeholder="请输入备注"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../JxcChuku.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();
  onMounted(() => {
    handleSearch();
  });
  const list3 = ref([]);
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    zcOnly: '',
    zcName: '',
    zcYuji: '',
    zcShiji: '',
    zcDanwei: '',
    zcOld: '',
    creatTime: '',
    zcLittle: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  function handleSearch() {
    console.log(121212);
    //自定义一个axios请求
    defHttp.get({ url: '/kcmx/jxcKcmx/getbh?companyId=' + userStore.getUserInfo.companyId }).then((res) => {
      // console.log(res);
      list3.value = res;
      console.log('-------', list3);
    });
  }
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    model.companyId = userStore.getUserInfo.companyId;
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
